<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class BWebshopShippingMethod  {

    var $db;
 	var $smarty;
 	
    function __construct($db, $smarty) {
    	$this->db = $db;
		$this->smarty = $smarty;
    }
    
    public function CreateShippingMethod() {
		$arr = array();
		$arr['code'] = 0;
		$arr['message'] = "";
		$arr['sEdit'] 	= _("Edit");
		$arr['sDelete'] = _("Delete");
	
		if($arr['code'] == 0) {
			$currency_id = $_POST['currency_id'];
			$currency = BWebshopProductBean::getCurrencyById($this->db, $currency_id);
			$symbol_left = $currency->symbol_left;
			$arr['name'] = $_POST['name'];
			$arr['delivery_cost'] = $_POST['delivery_cost'];
			$arr['symbol_left'] = $symbol_left;
			$arr['status'] = $_POST['status'];
			
			$ret = "";				 
			$ret = BWebshopShippingMethodBean::validateShippingMethod();
			if($ret == "") {
				$arr['ID'] = BWebshopShippingMethodBean::saveShippingMethod($this->db, $_POST);
			
				if( $arr['ID'] != -1) {
					$arr['code'] = 0;
					$arr['message'] = _("New Shipping method has been saved.");
					header("Location: ./?module=BgcmsWebshop&sub1=shippingmethod");
					return;
				} else {
					$arr['code'] = 4;
					$arr['message'] = _("ERROR: Could not save Shipping method to database.");
				}
			} else {
				$arr = array('ID' => -1, 'message' => $ret);
			}
		}
		
		//echo array2json($arr);
	}
	
	public function EditShippingMethod() {
		if(isset( $_GET['id'])) {
			$msgId = 2;
			$msg = "";				 
			$msg = BWebshopShippingMethodBean::validateShippingMethod();
			if($msg == "") {
				$shippingmethod = BWebshopShippingMethodBean::getShippingMethodsByCode($this->db, $_GET['id']);
				$code = $shippingmethod->code;
				$res = BWebshopShippingMethodBean::updateShippingMethod($this->db, $_POST, $code);
					
				$msgId = 1;
				header("Location: ./?module=BgcmsWebshop&sub1=shippingmethod&message=" . $msgId);
				return;

			} else {
				//we set msgId to 99 to indicate message will load in session variable
				$msgId = 99;
				$_SESSION['error_msg'] = $msg;
			}
			header("Location: ./?module=BgcmsWebshop&sub1=shippingmethod&act=editshippingmethod&message=" . $msgId . "&id=" . $_GET['id']);
		}
	}
	
	public function DeleteShippingMethod() {
		$arr = array('code' => 0, 'message' => "");
		$id = @$_POST['id'];
		$res = BWebshopShippingMethodBean::deleteShippingMethodByCode($this->db, $id);
		if($res == true) {
			$arr['code'] = 0;
			$arr['message'] = sprintf(_("Shipping method <u>%s</u> has been deleted."), $id);
		} else {
			$arr['code'] = -1;
			$arr['message'] = sprintf(_("Could not delete shipping method <u>%s</u>."), $id);
		}
		echo array2json($arr);
	}
}
?>